package cn.kim.edu.info.manager.dao;

import cn.kim.edu.info.manager.domain.Teacher;

public class TeacherDao {

    //创建数组
    private static Teacher[] teachers = new Teacher[5];

    public boolean addTeacher(Teacher teacher) {
        //先定义索引假设存满了
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher1 = teachers[i];
            if (teacher1 == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            teachers[index] = teacher;
            return true;
        }
    }

    //获取所有的数组元素信息
    public Teacher[] findAllTeacher() {
        return teachers;
    }

    //在方法层实现删除
    public void deleteTeacherById(String id) {
        //查询id在集合中的索引位置
        int index = getIndex(id);
        //将该索引位置的元素。使用null替换
        teachers[index] = null;
    }

    //抽离获取索引的方法
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher t = teachers[i];
            if (t != null && t.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    //具体实现修改方法
    public void updateTeacher(String id, Teacher newTeacher) {
        int index = getIndex(id);
        teachers[index] = newTeacher;
    }
}
